package com.byx.chatclient.model;

import android.content.Context;

import com.byx.chatclient.contract.home.ChatTaskContract;
import com.byx.chatclient.entity.MessagePo;
import com.byx.chatclient.factory.ManagerFactory;
import com.byx.chatclient.manager.UserChatRecordManager;
import com.byx.chatclient.manager.UserGroupManager;
import com.byx.chatclient.manager.UserManager;

import java.util.List;

/**
 * ChatModel
 *
 * @author Superb
 * @date 2021/3/27 22:06
 */
public class ChatModel implements ChatTaskContract.Model {
    private final UserChatRecordManager userChatRecordManager;
    private final UserGroupManager userGroupManager;
    private final UserManager userManager;

    public ChatModel(Context context) {
        this.userChatRecordManager = ManagerFactory.getInstance().getUserChatManager(context);
        this.userGroupManager = ManagerFactory.getInstance().getUserGroupManager(context);
        this.userManager = ManagerFactory.getInstance().getUserManager(context);
    }

    /**
     * 保存聊天信息
     *
     * @param userChat 聊天消息
     */
    @Override
    public void saveChatMessage(MessagePo userChat) {
        userChatRecordManager.saveOrUpdate(userChat);
        System.out.println("保存记录到本地成功");
    }

    /**
     * 根据好友id查询与其聊天的记录
     * @param userId 当前用户id
     * @param friendId 好友id,也可以是群id
     * @return 聊天记录
     */
    @Override
    public List<MessagePo> getChatMessage(String userId, String friendId) {
        return userChatRecordManager.queryChatRecord(userId,friendId);
    }

    @Override
    public List<MessagePo> getGroupChatMsg(String groupId) {
        return userChatRecordManager.queryGroupChatRecord(groupId);

    }

    public void deleteOneMessage(String msgId){
        userChatRecordManager.deleteByKey(msgId);
    }

}
